package com.rp.article.controller;


import com.rp.article.config.RabbitMQConfig;
import com.rp.article.config.RabbitMQDelayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;

@Controller
@Slf4j
public class RabbitMQTestController {



    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/send")
    public String send(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGER_ARTICLE,"article.test","这是一个测试");
        return "ok";
    }



    @GetMapping("/delay")
    public String delay(){
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties()
                        .setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                message.getMessageProperties()
                        .setDelay(5000);//单位是毫秒
                return message;
            }
        };
        rabbitTemplate.convertAndSend(RabbitMQDelayConfig.EXCHANGER_DELAY,"delay.test","这是一个延迟消息测试",messagePostProcessor);

        log.info("->{}",new Date());
        return "ok";
    }


}
